<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>	
<%@page import="com.yaodian100.mall.member.util.UserData"%>
<%@page import="com.yaodian100.core.common.utils.ServerValue"%>
<%@page import="org.apache.commons.lang.StringUtils"%>
<%@taglib uri="http://java.sun.com/jstl/core_rt" prefix="c-rt"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %> 	
<link rel="stylesheet" href="/css/yaodian100_ordercar_v2.css" type="text/css" media="screen" />
<script type="text/javascript" charset="UTF-8" src="/js/jquery.cascade.ext.js"></script>
<script type="text/javascript" charset="UTF-8" src="/js/jquery.cascade.js"></script>
<script type="text/javascript" charset="UTF-8" src="/js/jquery.templating.js"></script>
<script language="javascript">
$().ready(function(){
	$("#dialog").dialog({
		bgiframe: true,
		height: 200,
		width:700,
		modal: true
	});
	$("#submitDialog").click(function(){
			$("input[type=radio]").each(function(){
				if(this.checked){
					var abId=$(this).attr("id");
					if(abId != ''){
						$.getJSON("<%=ServerValue.getFrontContextPath()%>/shopping/orderInfo/findAddBook.do?type=json", 
								{abid:abId,adbk:"Y"}
							,function(addBookDataJson){
								$("#contactName").val(addBookDataJson.contactName).css("color","#000");
								$("#addr").val(addBookDataJson.addr).css("color","#000");
								$("#zipCode").val(addBookDataJson.zipCode);								
								if(addBookDataJson.mobile==null){
									$("#mobile").val(" ");
								}else{
									$("#mobile").val(addBookDataJson.mobile);
								}
								if(addBookDataJson.areaCode==null){
									$("#areaCode").val(" ");
								}else{
									$("#areaCode").val(addBookDataJson.areaCode);
								}
								if(addBookDataJson.phone==null){
									$("#phone").val(" ");
								}else{
									$("#phone").val(addBookDataJson.phone);
								}
								if(addBookDataJson.extension==null){
									$("#extension").val(" ");
								}else{
									$("#extension").val(addBookDataJson.extension);
								}
								
								$("#province").children().each(function(){
									if (trim($(this).text()) == trim(addBookDataJson.province)){
										$(this).attr("selected","true"); 
										queryProvince2(addBookDataJson.city,addBookDataJson.district);
								}
								});
								
						});
					}
				}
			});		
			$('#cssFlag').removeClass('consignee');
		closeDialog();
	});
});

//去除字串左邊的空白虛格
function ltrim(instr){
return instr.replace(/^[\s]*/gi,"");
}

//去除字串右邊的空白虛格
function rtrim(instr){
return instr.replace(/[\s]*$/gi,"");
}

//去除字串前後的空白虛格
function trim(instr){
instr = ltrim(instr);
instr = rtrim(instr);
return instr;
}

//查詢province ajax
function queryProvince2(adCity,adDistrict){
	var province=$("#province").find('option:selected').val();
	$.post("<%=ServerValue.getFrontContextPath()%>/shopping/addressdata/findOrderCitys.do",{id:$("#orderId").val(),province:province},callback,"json");
	function callback(actionResult){
		addressObj(actionResult,'CITY',adCity,adDistrict);
	}
}

//Province&City 查詢元件
function addressObj(actionResult,execObj,adCity,adDistrict){
	if(actionResult!=null ){
				var arr_text = actionResult.split("$");
				if(execObj=='CITY'){								
					var options = '<option value="">== 选择城市 ==</option>';
				}
				if(execObj=='DISTRICT'){
					var options = '<option value="">== 选择地区 ==</option>';
				}
				 for(i=0;i<arr_text.length;i++)
				 {
					 v=arr_text[i].split(",");
					 if(execObj=='CITY'){	
						options += '<option value="' + v[1] + '">' + v[1] + '</option>';
					 }
					if(execObj=='DISTRICT'){
						options += '<option value="' + v[1]+','+v[2]+','+v[3] +','+v[4]+ '">' + v[1] + '</option>';
					}
				 }
				 var aObj;
				 if(execObj=='CITY'){	
					 $("#city").html(options);
					 aObj=$("#city");
				 }
				 if(execObj=='DISTRICT'){
					 $("#district").html(options);
					 aObj=$("#district");
				 }
					 aObj[0].selectedIndex = 0 ; 
					var tmpObj;
					if(execObj=='CITY'){	
						tmpObj=adCity;
					 }
					 if(execObj=='DISTRICT'){
						 tmpObj=adDistrict;
					 }
					 if(tmpObj !=""){
						 aObj.children().each(function(index){
							 if(execObj=='CITY'){	
								 if ($(this).text()==adCity){
								       setTimeout(function() {  aObj[0].selectedIndex = index ; 
								       queryCity2(adDistrict);} , 1);
								    }
								 }
								 if(execObj=='DISTRICT'){
									 if ($(this).text().substring(1)==adDistrict || $(this).text()==adDistrict){
									    	setTimeout(function() {  $("#district")[0].selectedIndex = index ;
									    	queryDistinct();},1);
									    }
								 }
						 });
					 }
	}
}

//查詢city ajax
function queryCity2(adDistrict){
	var city=$("#city").find('option:selected').val();
	$.post("<%=ServerValue.getFrontContextPath()%>/shopping/addressdata/findOrderDistrictsCod.do",{id:$("#orderId").val(),city:city},callback,"json");
	function callback(actionResult){
		addressObj(actionResult,'DISTRICT','',adDistrict);
	}
}

//查詢distinct ajax
function queryDistinct(){
	var districtVal = $("#district").find('option:selected').val();
	var districtTxt = $("#district").find('option:selected').text();
	var dtVal=districtVal.split(",");
}

</script>
 <DIV id="dialog" title="收货地址簿">
  <span>请选择收货人：</span>
  <div class="f-rt txt-rd">*仅显示该商品能配送到达的地址</div>
	<table width="100%" border="0" class="tb-2">
  <tr>
    <th width="5%"></th>
    <th width="15%">收货人</th>
    <th width="50%">地址</th>
    <th width="15%">手机号码</th>
    <th width="15%">固定电话</th>
  </tr>
   <c-rt:forEach items="${abList}" var="addbook" varStatus="rows_index"> 
   		<tr>${row_index.count }
        <td><input type="radio" name="abid" id="${addbook.id}" value="${addbook.id}"/></td>
	    <td>${addbook.contactName}</td>
	    <td>${addbook.province},${addbook.city},${addbook.district},${addbook.addr} ,${addbook.zipCode}</td>
	    <td>${addbook.mobile}</td>
	    <td>${addbook.areaCode}${addbook.phone} ${addbook.extension}</td>
	    </tr>
   </c-rt:forEach>
  
</table>
<input type="hidden" id="orderId" value="${orderId}"/>
<p class="c-f"  align="center"><br /><a id="submitDialog" href="javascript:;" class="btn-mid" onFocus="blur()">确定</a></p>
  </DIV>
